package org.university.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.university.common.ApiResponse;
import org.university.entity.ClassroomAssignment;
import org.university.exception.ResourceNotFoundException;
import org.university.service.ClassroomAssignmentService;

/**
 * 【教室分配】
 */
@CrossOrigin
@RestController
@RequestMapping("/classroomAssignments")
public class ClassroomAssignmentController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ClassroomAssignmentController.class);
    private final ClassroomAssignmentService classroomAssignmentService;

    public ClassroomAssignmentController(ClassroomAssignmentService classroomAssignmentService) {
        this.classroomAssignmentService = classroomAssignmentService;
    }

    /**
     * 获取【教室分配】信息分页
     *
     * @param page 页码
     * @param size 页面大小
     * @return 信息分页
     */
    @GetMapping
    public ResponseEntity<ApiResponse<Page<ClassroomAssignment>>> getClassroomAssignment(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size
    ) {
        LOGGER.info("查询【教室分配】：页码={}, 每页大小={}", page, size);
        Page<ClassroomAssignment> classroomAssignments = classroomAssignmentService.findAll(PageRequest.of(page, size));
        return ApiResponse.success(classroomAssignments);
    }

    /**
     * 根据ID查找【教室分配】
     */
    @GetMapping("/{id}")
    public ResponseEntity<ApiResponse<ClassroomAssignment>> getClassroomAssignmentById(@PathVariable Integer id) {
        LOGGER.info("查询【教室分配】ID: {}", id);
        return classroomAssignmentService.findById(id)
                .map(ApiResponse::success)
                .orElseThrow(() -> new ResourceNotFoundException("未找到id为：" + id + " 的【教室分配】"));
    }

    /**
     * 创建【教室分配】
     */
    @PostMapping
    public ResponseEntity<ApiResponse<ClassroomAssignment>> createClassroomAssignment(@RequestBody ClassroomAssignment classroomAssignment) {
        LOGGER.info("创建【教室分配】ID: {}", classroomAssignment.getAssignmentId());
        ClassroomAssignment newClassroomAssignment = classroomAssignmentService.save(classroomAssignment);
        return ApiResponse.created(newClassroomAssignment);
    }

    /**
     * 更新【教室分配】信息
     */
    @PutMapping("/{id}")
    public ResponseEntity<ApiResponse<ClassroomAssignment>> updateClassroomAssignment(
            @PathVariable Integer id,
            @RequestBody ClassroomAssignment classroomAssignment) {

        LOGGER.info("更新【教室分配】ID: {}", id);
        if (classroomAssignmentService.findById(id).isEmpty()) {
            return ApiResponse.error(HttpStatus.NOT_FOUND, new String("未找到id为：" + id + " 的【教室分配】"));
        }
        classroomAssignment.setAssignmentId(id);
        ClassroomAssignment updatedClassroomAssignment = classroomAssignmentService.save(classroomAssignment);
        return ApiResponse.success(updatedClassroomAssignment);
    }

    /**
     * 删除【教室分配】
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<ApiResponse<Void>> deleteClassroomAssignment(@PathVariable Integer id) {
        LOGGER.info("删除【教室分配】ID: {}", id);
        if (classroomAssignmentService.findById(id).isEmpty()) {
            return ApiResponse.error(HttpStatus.NOT_FOUND, new String("未找到id为：" + id + " 的【教室分配】"));
        }
        classroomAssignmentService.deleteById(id);
        return ResponseEntity.ok().build();
    }
}
